import { _path, existplayer, Write_player } from '../../mode/allapi'
import { obtainingImages, RedisA, plugin, AMessage } from '../../api/api'

export class game extends plugin {
  constructor() {
    super({
      dsc: '面板信息',
      rule: [
        {
          reg: /^(#|\/)?仓库$/,
          fnc: 'cangku'
        },
        {
          reg: /^(#|\/)?阵法$/,
          fnc: 'zhen'
        },
        {
          reg: /^(#|\/)?兵营$/,
          fnc: 'bingyin'
        },
        {
          reg: /^(#|\/)?政厅$/,
          fnc: 'MYxinxi'
        },
        {
          reg: /^(#|\/)?点卯$/,
          fnc: 'fuli'
        },
        {
          reg: /^(#|\/)?改名.*$/,
          fnc: 'changeName'
        }
      ]
    })
  }
  async cangku(e: AMessage) {
    const id = e.user_id
    if (!(await existplayer(id))) return false
    const data = {
      MyData: await (await _path(id)).cangku,
      user_id: id
    }
    const img = await obtainingImages(
      '/resources/html/cangku/cangku.html',
      data
    )
    if (img) e.reply(img)
    return true
  }

  async zhen(e: AMessage) {
    const id = e.user_id
    if (!(await existplayer(id))) return e.reply('请先创建存档')
    const player = await (await _path(id)).player
    const ji = player.wujian.map(obj =>
      obj && obj.name !== undefined && obj.hp !== undefined
        ? `${obj.level}`
        : ' '
    )
    const zhen = player.wujian.map(obj =>
      obj && obj.name !== undefined && obj.hp !== undefined
        ? `${obj.name} (${obj.hp})`
        : ' '
    )
    const data = {
      Zhen: zhen,
      dengji: ji
    }
    const img = await obtainingImages('/resources/html/zhen/zhen.html', data)
    if (img) e.reply(img)
    return true
  }

  async bingyin(e: AMessage) {
    const id = e.user_id
    if (!(await existplayer(id))) return e.reply('请先创建存档')
    const bingying = await (await _path(id)).bingyin
    for (let i = 0; i < bingying.length; i += 15) {
      const chunk = bingying.slice(i, i + 15)
      const data = {
        MyData: chunk,
        user_id: id
      }
      const img = await obtainingImages('/resources/html/bingying/bingying.html', data)
      if (img) e.reply(img)
    }
    return true
  }
  async MYxinxi(e: AMessage) {
    const id = e.user_id
    if ((await existplayer(id)) != true) return e.reply('请先创建存档')
    const data = {
      HData: await (await _path(id)).player
    }
    const img = await obtainingImages(
      '/resources/html/player/player.html',
      data
    )
    if (img) e.reply(img)
    return true
  }
  async fuli(e: AMessage) {
    const id = e.user_id
    const nowTime1 = new Date().getTime() 
    const nowTime = new Date(nowTime1)
    if ((await existplayer(id)) != true) return false
    const obj = await RedisA.get('player' + id + ':lastsign_time')
    const time = new Date(parseInt(obj))
    const day = time.getDate()
    const yea = time.getMonth() + 1
    const nian = time.getFullYear()
    if (
      day == nowTime.getDate() &&
      yea == nowTime.getMonth() + 1 &&
      nian == nowTime.getFullYear()
    ) {
      e.reply('今日领取过')
      return true
    }
    await RedisA.set('player' + id + ':lastsign_time', nowTime1) 
    const player = await (await _path(id)).player
    player.currency_num += 1000
    await Write_player(id, player)
    e.reply(`${e.segment.at(id)}签到成功获得1000五铢钱`)
  }
  async changeName(e: AMessage) {
    const id = e.user_id
    if ((await existplayer(id)) != true) return false
    const player = await (await _path(id)).player
    player.name = e.msg.replace(/^(#|\/)?改名/, '')
    await Write_player(id, player)
    const data = {
      HData: player
    }
    const img = await obtainingImages(
      '/resources/html/player/player.html',
      data
    )
    if (img) e.reply(img)
    return true
  }
}
